//
//  CNNetworkTool.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/21.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import AFNetworking

// 统一管理错误信息
enum CNNetWorkError: Int {
    case RepeatRefresh = -1
    case EmptyAccessToken = -2
    
    var netDomain: String {
        get {
            return "NETWORKERROR"
        }
    }
    
    var description: String {
        get {
            switch self {
            case RepeatRefresh:
                return "重复刷新"
            case EmptyAccessToken:
                return "AccessToken不能为空"
            }
        }
    }
    
    func error() -> NSError {
        return NSError(domain: netDomain, code: self.rawValue, userInfo: ["ErrorDescription" : self.description])
    }
}

class CNNetworkTool: NSObject {
    
    static let sharedInstance:CNNetworkTool = CNNetworkTool()
    
    private let afnManager: AFHTTPSessionManager = AFHTTPSessionManager(baseURL: baseURL)
    
    private override init() {
        
        super.init()
        // 设置afn支持text/plain类型
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    // 发送GET请求
    func sendGetRequest(urlString: String, var parameters: [String: AnyObject]?, completion: ((result: [String: AnyObject]?, error: NSError?)->())) {
        // 封装access_token,使用者不需要在传入的字典中加入
        guard let access_token = CNUserAccountViewModel.sharedInstance.userAccount?.access_token else {
            completion(result: nil, error: CNNetWorkError.EmptyAccessToken.error())
            return
        }
        if parameters == nil {
            parameters = [String: AnyObject]()
        }
        parameters!["access_token"] = access_token
        
        afnManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            let dict = result as? [String: AnyObject]
            completion(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                
                completion(result: nil, error: error)
        }
    }
    // 发送POST请求
    func sendPostRequest(urlString: String, parameters: AnyObject?, completion: ((result: [String: AnyObject]?, error: NSError?)->())) {
        
        afnManager.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            let dict = result as? [String: AnyObject]
            completion(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                
                completion(result: nil, error: error)
        }
    }
    func sendPostRequest(urlString: String, parameters: [String: AnyObject], constructingBodyWithBlock: ((AFMultipartFormData) -> Void)?, completion: ((result: [String: AnyObject]?, error: NSError?)->())) {
        
        afnManager.POST(urlString, parameters: parameters, constructingBodyWithBlock: constructingBodyWithBlock, progress: nil, success: { (_, result) -> Void in
            
            let dict = result as? [String: AnyObject]
            completion(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                
                completion(result: nil, error: error)
        }
    }
}
